package com.dusk.encrypt
{
	import com.hurlant.crypto.Crypto;
	import com.hurlant.util.Hex;
	
	import flash.utils.ByteArray;
	
	public class HashEncrypt
	{
		public static function verifyData(data:*, salt:String, hashValue:String):Boolean
		{
			return hash(data, salt) == hashValue;
		}
		
		/**
		 * 计算hash值，采用HMAC sha256算法
		 * @param data 只能为String或者ByteArray
		 * @param salt 密码，只能为String
		 * @return 16进制字符串格式的hash值
		 */
		public static function hash(data:*, salt:String):String
		{
			if (data is String)
			{
				var tmp:String = data;
				data = new ByteArray();
				data.writeUTFBytes(tmp);
			}
			if (!(data is ByteArray))
			{
				throw (new Error("get hash error! invalid data format"));
			}
			var ba:ByteArray = new ByteArray();
			ba.writeUTFBytes(salt.toString());
			var hash:ByteArray = Crypto.getHMAC("sha256").compute(ba, data);
			return Hex.fromArray(hash);
		}
	}
}
